<template>
  <div class="games-page">
    <canvas id="projectBackgroundCanvas"></canvas>
    <!-- 添加返回按钮 -->
    <button @click="goBack" class="back-button">
      <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
        <polyline points="15 18 9 12 15 6"></polyline>
      </svg>
    </button>

    <h1 class="page-title">游戏展示</h1>
    
    <!-- 添加展示游戏的卡片容器 -->
    <div class="game-cards-container">
      <!-- 左侧切换按钮 -->
      <button @click="prevGame" class="switch-button left-button" :disabled="games.length <= 1">
        &lt;
      </button>
      
      <!-- 左侧游戏卡片（20%） -->
      <div class="game-card left-card" @click="navigateToGame(currentLeftGame.url, currentLeftGame.id)" v-if="games.length > 1">
        <img 
          :src="currentLeftGame.image" 
          :alt="currentLeftGame.title" 
          class="game-image"
        />
        <div class="game-info">
          <h2 class="game-title">{{ currentLeftGame.title }}</h2>
          <p class="game-description">{{ currentLeftGame.description }}</p>
        </div>
      </div>
      
      <!-- 添加展示当前主游戏的卡片（60%） -->
      <div 
        class="game-card main-card" 
        :class="{ 'single-game': games.length === 1 }"
        @click="navigateToGame(currentMainGame.url, currentMainGame.id)"
        @touchstart="handleTouchStart"
        @touchmove="handleTouchMove"
        @touchend="handleTouchEnd">
        <img 
          :src="currentMainGame.image" 
          :alt="currentMainGame.title" 
          class="game-image"
        />
        <div class="game-info">
          <h2 class="game-title">{{ currentMainGame.title }}</h2>
          <p class="game-description">{{ currentMainGame.description }}</p>
        </div>
      </div>
      
      <!-- 右侧游戏卡片（20%） -->
      <div class="game-card right-card" @click="navigateToGame(currentRightGame.url, currentRightGame.id)" v-if="games.length > 1">
        <img 
          :src="currentRightGame.image" 
          :alt="currentRightGame.title" 
          class="game-image"
        />
        <div class="game-info">
          <h2 class="game-title">{{ currentRightGame.title }}</h2>
          <p class="game-description">{{ currentRightGame.description }}</p>
        </div>
      </div>
      
      <!-- 右侧切换按钮 -->
      <button @click="nextGame" class="switch-button right-button" :disabled="games.length <= 1">
        &gt;
      </button>
    </div>
    
    <!-- 添加状态指示器 -->
    <div class="indicators" v-if="games.length > 1">
      <span 
        v-for="(game, index) in sortedGames" 
        :key="game.id" 
        :class="{ 'active': index === currentIndex }" 
        class="indicator">
      </span>
    </div>
  </div> 
</template>

<script>
import planeGame from '@/assets/gaming/planeGame.png';
import img2048 from '@/assets/gaming/2048.png';
import tetris from '@/assets/gaming/tetris.png';
import { initProjectBackground, enableProjectBackground, disableProjectBackground } from '@/utils/projectBackground.js';

export default {
  name: 'GamePage',
  data() {
    return {
      currentIndex: 0, // 当前主游戏索引
      currentGameId: null, // 当前居中游戏的ID
      games: [
        {
          id: 1,
          title: '飞机大战',
          description: '经典的飞机射击游戏，躲避敌机并摧毁它们',
          image: planeGame,
          url: '/game/plane'
        },
        {
          id: 2,
          title: '2048',
          description: '经典的益智小游戏，合并相同数字来获得更高的分数',
          image: img2048,
          url: '/game/2048'
        },
        {
          id: 3,
          title: '俄罗斯方块',
          description: '经典的俄罗斯方块游戏，通过移动和旋转方块来消除行',
          image: tetris, // 使用导入的图片
          url: '/game/tetris'
        }
      ],
      // 添加触摸相关数据
      touchStartX: 0,
      touchEndX: 0,
      isTouching: false
    };
  },
  computed: {
    // 确保游戏按ID排序
    sortedGames() {
      return [...this.games].sort((a, b) => a.id - b.id);
    },
    
    // 获取当前主游戏
    currentMainGame() {
      return this.sortedGames[this.currentIndex];
    },
    
    // 获取当前左侧游戏（上一个游戏，循环）
    currentLeftGame() {
      // 只有当游戏数量大于1时才计算左侧游戏
      if (this.sortedGames.length > 1) {
        const prevIndex = (this.currentIndex - 1 + this.sortedGames.length) % this.sortedGames.length;
        return this.sortedGames[prevIndex];
      }
      return null;
    },
    
    // 获取当前右侧游戏（下一个游戏，循环）
    currentRightGame() {
      // 只有当游戏数量大于1时才计算右侧游戏
      if (this.sortedGames.length > 1) {
        const nextIndex = (this.currentIndex + 1) % this.sortedGames.length;
        return this.sortedGames[nextIndex];
      }
      return null;
    }
  },
  created() {
    // 从sessionStorage恢复保存的游戏ID
    const savedGameId = sessionStorage.getItem('currentGameId');
    if (savedGameId) {
      this.currentGameId = parseInt(savedGameId, 10);
      
      // 根据保存的游戏ID设置当前索引
      const gameIndex = this.sortedGames.findIndex(game => game.id === this.currentGameId);
      if (gameIndex !== -1) {
        this.currentIndex = gameIndex;
      }
    }
  },
  mounted() {
    // 启用项目背景渲染和鼠标监听
    enableProjectBackground();
  },
  beforeUnmount() {
    // 禁用项目背景渲染和鼠标监听
    disableProjectBackground();
  },
  methods: {
    // 添加返回方法
    goBack() {
      this.$router.push('/');
    },

    navigateToGame(url, gameId) {
      // 保存当前游戏ID
      this.currentGameId = gameId;
      sessionStorage.setItem('currentGameId', gameId.toString());
      
      this.$router.push(url);
    },
    
    // 切换到下一个游戏
    nextGame() {
      // 只有当游戏数量大于1时才切换
      if (this.sortedGames.length > 1) {
        this.currentIndex = (this.currentIndex + 1) % this.sortedGames.length;
        
        // 保存当前居中游戏的ID
        this.currentGameId = this.currentMainGame.id;
        sessionStorage.setItem('currentGameId', this.currentGameId.toString());
      }
    },
    
    // 切换到上一个游戏
    prevGame() {
      // 只有当游戏数量大于1时才切换
      if (this.sortedGames.length > 1) {
        this.currentIndex = (this.currentIndex - 1 + this.sortedGames.length) % this.sortedGames.length;
        
        // 保存当前居中游戏的ID
        this.currentGameId = this.currentMainGame.id;
        sessionStorage.setItem('currentGameId', this.currentGameId.toString());
      }
    },
    
    // 添加触摸事件处理方法
    handleTouchStart(event) {
      this.isTouching = true;
      this.touchStartX = event.touches[0].clientX;
    },
    
    handleTouchMove(event) {
      if (!this.isTouching) return;
      this.touchEndX = event.touches[0].clientX;
    },
    
    handleTouchEnd() {
      if (!this.isTouching) return;
      this.isTouching = false;
      
      const deltaX = this.touchStartX - this.touchEndX;
      const MIN_SWIPE_DISTANCE = 50;
      
      // 向左滑动 - 切换到下一个游戏
      if (deltaX > MIN_SWIPE_DISTANCE) {
        this.nextGame();
      }
      // 向右滑动 - 切换到上一个游戏
      else if (deltaX < -MIN_SWIPE_DISTANCE) {
        this.prevGame();
      }
    }
  }
};
</script>
<style>
/* 添加返回按钮样式 */
.back-button {
  position: absolute;
  top: 20px;
  left: 20px;
  background: rgba(0, 0, 0, 0.5);
  color: white;
  border: none;
  border-radius: 5px;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  z-index: 100;
  transition: background 0.3s;
}

.back-button:hover {
  background: rgba(0, 0, 0, 0.7);
}

/* 页面特定样式 */
.games-page {
  min-height: 100vh;
  color: #fff;
  text-align: center;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px 0;
}

/* 背景画布样式 */
#projectBackgroundCanvas {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

/* 游戏卡片容器样式 */
.game-cards-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  max-width: 1200px;
  margin: 20px auto;
  perspective: 1000px;
  gap: 0; /* 移除默认间隙 */
  height: 500px; /* 设置固定高度避免浮动 */
}

/* 切换按钮样式 */
.switch-button {
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: none;
  border-radius: 50%;
  width: 50px;
  height: 50px;
  font-size: 1.5rem;
  cursor: pointer;
  margin: 0 10px;
  transition: all 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  align-self: center; /* 垂直居中 */
}

.switch-button:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.1);
}

.switch-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* 游戏卡片样式 */
.game-card {
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  cursor: pointer;
  transition: transform 0.3s, box-shadow 0.3s;
  display: flex;
  flex-direction: column;
  height: 450px; /* 设置固定高度 */
}

/* 主卡片样式 (60%) */
.game-card.main-card {
  flex: 6; /* 占据60%宽度 */
  max-width: none;
  margin: 0 10px; /* 添加适当的边距 */
  height: 480px; /* 增加高度，比两侧卡片高 */
  z-index: 5; /* 提高层级，确保在前 */
  position: relative; /* 为z-index生效添加定位 */
}

/* 当只有一个游戏时的主卡片样式 */
.game-card.main-card.single-game {
  flex: 6; /* 保持60%宽度 */
  max-width: 60%; /* 确保最大宽度为60% */
  margin: 0 auto; /* 居中显示 */
}

/* 左侧卡片样式 (20%) */
.game-card.left-card {
  flex: 2; /* 占据20%宽度 */
  max-width: none;
  transform: rotateY(-40deg) rotateZ(0deg) translateZ(-20px);
  position: relative;
  z-index: 2;
  transform-style: preserve-3d;
  margin: 0 10px; /* 添加适当的边距 */
  height: 450px; /* 设置固定高度 */
}

/* 右侧卡片样式 (20%) */
.game-card.right-card {
  flex: 2; /* 占据20%宽度 */
  max-width: none;
  transform: rotateY(40deg) rotateZ(0deg) translateZ(-20px);
  position: relative;
  z-index: 2;
  transform-style: preserve-3d;
  margin: 0 10px; /* 添加适当的边距 */
  height: 450px; /* 设置固定高度 */
}

.game-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 25px rgba(0, 0, 0, 0.4);
}

/* 为左侧卡片保持原有的悬停效果 */
.game-card.left-card:hover {
  transform: rotateY(-40deg) rotateZ(0deg) translateZ(-20px) translateY(-5px);
}

/* 为右侧卡片保持原有的悬停效果 */
.game-card.right-card:hover {
  transform: rotateY(40deg) rotateZ(0deg) translateZ(-20px) translateY(-5px);
}

.game-card .game-image {
  width: 100%;
  height: 250px; /* 设置固定高度 */
  object-fit: contain; /* 修改为contain以完整显示图片 */
  object-position: center; /* 图片居中显示 */
  display: block;
}

.game-info {
  padding: 20px;
  text-align: center;
  flex-grow: 1; /* 占据剩余空间 */
  display: flex;
  flex-direction: column;
  justify-content: center; /* 垂直居中内容 */
}

.game-info .game-title {
  font-size: 1.5rem;
  margin-bottom: 10px;
  color: #fff;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
}

.game-info .game-description {
  font-size: 1.1rem;
  color: rgba(255, 255, 255, 0.9);
  line-height: 1.6;
  flex-grow: 1; /* 占据可用空间 */
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 添加状态指示器样式 */
.indicators {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  gap: 10px;
}

.indicator {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: rgba(255, 255, 255, 0.3);
  transition: background-color 0.3s;
}

.indicator.active {
  background-color: white;
}

@media (max-width: 768px) {
  .game-cards-container {
    flex-direction: row; /* 保持水平排列 */
    height: 500px; /* 保持固定高度 */
    position: relative; /* 为按钮定位做准备 */
  }
  
  /* 隐藏切换按钮 */
  .switch-button {
    display: none;
  }

  .switch-button.left-button {
    left: 10px;
  }

  .switch-button.right-button {
    right: 10px;
  }

  .game-card.left-card,
  .game-card.right-card {
    display: none;
  }
  
  .game-card {
    height: 480px; /* 保持固定高度 */
    width: 80%;
    max-width: 500px;
    margin: 0 auto; /* 居中显示 */
  }

  .game-card.main-card {
    flex: none; /* 移除flex属性 */
    display: block; /* 确保显示 */
  }
  .game-card.main-card.single-game {
    max-width: 80%;
    margin: 0 auto;
  }
  
  .game-card.main-card,
  .game-card.left-card,
  .game-card.right-card {
    flex: none;
  }
  
  .game-card.main-card.single-game {
    max-width: 80%; /* 在移动端调整最大宽度 */
    margin: 10px auto; /* 在移动端调整边距 */
  }
  
  .game-card.left-card,
  .game-card.right-card {
    transform: rotateY(0);
  }
  
  .game-card.left-card:hover,
  .game-card.right-card:hover {
    transform: translateY(-5px);
  }
  
  .game-card .game-image {
    height: 250px; /* 在移动端调整图片高度 */
  }
  
  .game-info .game-title {
    font-size: 1.5rem;
  }
  
  .game-info .game-description {
    font-size: 1rem;
  }
  
  /* 在移动端调整状态指示器 */
  .indicators {
    margin-top: 30px;
  }
  
  .indicator {
    width: 8px;
    height: 8px;
  }
}
</style>